package com.songcha.library_business.handler.home_dialog

import android.app.Dialog
import android.content.Context
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

//the abstract class of handle home complicated dialog
abstract class AbsHomeDialog(protected val context: Context){
    protected var nextDialog: AbsHomeDialog?=null
        private set
    abstract fun getPriority():Int
    fun setNextDialog(dialog:AbsHomeDialog){
        nextDialog=dialog
    }
    abstract fun needShowDialog():Observable<Boolean>
    abstract fun showDialog()
    open fun handleShowDialog(){
        needShowDialog()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({needShow->
                if(needShow){
                    showDialog()
                    //perhaps same priority dialog
                    if(nextDialog!=null && nextDialog!!.getPriority()==getPriority()){
                        nextDialog!!.handleShowDialog()
                    }
                }else{
                    if(nextDialog!=null)
                        nextDialog!!.handleShowDialog()
                }
            },{

            })
    }

}